/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: gsingh $
 * $Revision: 1.2 $
 * $Date: 2004/06/01 02:46:15 $
 * (c)Copyright 2004
 * education.au limited 
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this 
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its 
 * contributors may be used to endorse or promote products derived from this 
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and 
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising 
 * in any way from the use of the software whether arising from or in 
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is 
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 * 
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.service;

import java.io.File;

import au.edu.educationau.opensource.dsm.obj.SearchCriteria;
import au.edu.educationau.opensource.dsm.util.Flog;
import au.edu.educationau.opensource.dsm.worker.SpellCheckTask;

import com.swabunga.spell.engine.SpellDictionary;
import com.swabunga.spell.engine.SpellDictionaryHashMap;

/**
 * Uses the Jazzy API to conduct event based spelling checks on the query terms.
 * Provides the Did you mean ____? service
 * 
 * This class requires overhauling. It returns straight word spell checking out
 * of the standard dictionary. Perhaps, ways of adding words that dont already
 * exist. Making sure that the resulting terms make sense: Example: Ned Kelly
 * returns Net Jelly
 */

public class SpellCheckService {

	private static final String dictFile = "english.0";
	private static final String phonetFile = "phonet.en";

	private static SpellCheckService _instance = null;
	private SpellDictionary dictionary = null;

	private SpellCheckService() {
		Flog.console(toString(), "Starting SpellCheckService Engine...");
		try {
			String dictFile = PropertiesService.getStringProperty("dsm4_dict_file", "./WEB-INF/etc/dict/english.0");
			String phonetFile = PropertiesService.getStringProperty("dsm4_phonet_file", "./WEB-INF/etc/dict/phonet.en");
			dictionary = new SpellDictionaryHashMap(new File(dictFile), new File(phonetFile));
			Flog.console(toString(), "SpellCheckService Engine started ok.");
		} catch (Exception e) {
			Flog.error(toString(), "Cannot initialise SpellCheckService engine", e);
		}
	}

	/** Returns the singleton SpellCheckService - use DeusExMachina to get it */
	public static SpellCheckService getInstance() {
		if (null == _instance) {
			_instance = new SpellCheckService();
		}
		return _instance;
	}

	/** Destroy objects worthy of destruction */
	public void destroy() {
		try {
			dictionary = null;
		} catch (Exception oe) {
		}
		Flog.debug(toString(), "SpellCheckService Engine shutdown ok.");
	}

	/**
	 * Build the spell check task from the search criteria
	 * 
	 * @param criteria
	 */
	public SpellCheckTask buildSpellCheckTask(SearchCriteria criteria) {
		return new SpellCheckTask(criteria.getToken(), criteria.getQuery(), dictionary);
	}

	/** Displays the properly formatted class string */
	public String toString() {
		return "o.m.d.m.SpellCheckService";
	}

} // class SpellCheckService

// -- EOF
